package com.car.controller;

import com.car.pojo.Car;
import com.car.pojo.CarQueryParam;
import com.car.pojo.PageResult;
import com.car.pojo.Result;
import com.car.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 汽车控制器
 */
@Slf4j
@RestController
public class CarController {

    @Autowired
    private CarService carService;

    /**
     * 根据用户ID查询车辆列表
     */
    @GetMapping("/cars/user/{userId}")
    public Result getUserCars(@PathVariable Integer userId) {
        log.info("根据用户ID查询车辆列表: userId={}", userId);
        List<Car> cars = carService.getCarsByUserId(userId);
        return Result.success(cars);
    }

    @GetMapping("/cars/usercount/{userId}")
    public Result getUserCarsCount(@PathVariable Integer userId) {
        log.info("根据用户ID查询车辆数量: userId={}", userId);
        Integer count = carService.getCarsCountByUserId(userId);
        return Result.success(count);
    }

    /**
     * 添加用户车辆
     */
    @PostMapping("/cars/user")
    public Result addUserCar(@RequestBody Car car) {
        log.info("添加用户车辆: {}", car);
        
        // 检查车牌号是否已存在
        boolean exists = carService.isPlateNumberExists(car.getPlateNumber(), null, car.getUserId());
        if (exists) {
            return Result.error("车牌号已存在");
        }
        
        carService.addCar(car);
        return Result.success();
    }

    /**
     * 更新用户车辆信息
     */
    @PutMapping("/cars/user")
    public Result updateUserCar(@RequestBody Car car) {
        log.info("更新用户车辆信息: {}", car);
        
        // 检查车牌号是否已存在（排除当前车辆）
        boolean exists = carService.isPlateNumberExists(car.getPlateNumber(), car.getId(), car.getUserId());
        if (exists) {
            return Result.error("车牌号已存在");
        }
        
        carService.updateCar(car);
        return Result.success();
    }

    /**
     * 删除用户车辆
     */
    @DeleteMapping("/cars/user/{id}")
    public Result deleteUserCar(@PathVariable Integer id) {
        log.info("删除用户车辆: id={}", id);
        carService.deleteCar(id);
        return Result.success();
    }

    /**
     * 分页查询汽车
     */
    @GetMapping("/cars")
    public Result page(CarQueryParam param) {
        log.info("分页查询汽车: {}", param);
        PageResult<Car> pageResult = carService.page(param);
        return Result.success(pageResult);
    }

    /**
     * 新增汽车
     */
    @PostMapping("/cars")
    public Result save(@RequestBody Car car) throws Exception {
        log.info("新增汽车: {}", car);
        carService.save(car);
        return Result.success();
    }

    /**
     * 删除汽车
     */
    @DeleteMapping("/cars")
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除汽车: {}", ids);
        carService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID查询汽车信息
     */
    @GetMapping("/cars/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据ID查询汽车信息: {}", id);
        Car car = carService.getInfo(id);
        return Result.success(car);
    }

    /**
     * 修改汽车
     */
    @PutMapping("/cars")
    public Result update(@RequestBody Car car) {
        log.info("修改汽车: {}", car);
        carService.update(car);
        return Result.success();
    }

    /**
     * 查询所有汽车
     */
    @GetMapping("/cars/list")
    public Result list() {
        log.info("查询所有汽车");
        List<Car> carList = carService.list();
        return Result.success(carList);
    }
}